package Tk02;

public class TicketHouse implements Runnable{


    int fiveAccount=2;
    int tenAccount=1;
    int twentyAccount=0;

    public void run(){

        String name=Thread.currentThread().getName();
        if(name.equals("张飞")){
            saleTicket(20);
        }

        else if(name.equals("李逵")){
            saleTicket(5);
        }

    }

    public synchronized void saleTicket(int money){
        String name=Thread.currentThread().getName();
        if(money==5){
            fiveAccount++;
            System.out.println("给"+name+"，入场卷"+name+"钱正好！");
        }else if(money==20){
            while(fiveAccount<3) {
                try {
                    System.out.println(name + "靠边等");
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(name + "继续买票");
            }
            fiveAccount-=3;
            twentyAccount+=1;
            System.out.println("给"+name+" 入场卷"+"给20元，找15元");
        }

        notifyAll();
    }


}
